import 'package:flutter/material.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:ichinesereader/controller/reports.dart';
import 'package:ichinesereader/models/report/basketsResult.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:ichinesereader/widgets/grids/basketDataGrid.dart';
import 'package:get/get.dart';
import '../../themes/pageStyles.dart';

class StudentBasketsPage extends StatefulWidget {
  static const String routeName = '/student/report/baskets';

  @override
  State<StudentBasketsPage> createState() => _StudentBasketsPageState();
}

class _StudentBasketsPageState extends State<StudentBasketsPage> {
  final box = Hive.box('appconfig');
  final ReportsController controller = Get.find();
  var basketData = <BasketItem>[];

  @override
  void initState() {
    bool isStudent = box.get(CacheManagerKey['nanhairole'].toString()) ==
        Constants.NanhaiRole_Student;
    if (isStudent) {
      controller.fetchStudentBaskets().then((data) {
        setState(() {
          basketData = data;
        });
      });
      // Future.delayed(Duration(milliseconds: 200), (() {
      //   controller.fetchStudentBaskets();
      // }));
    }
    super.initState();
  }

  final PageStyles _pageStyles = PageStyles();

  @override
  Widget build(BuildContext context) {
    Log.debug('build student BasketsPage');
    return Scaffold(
      backgroundColor: Colors.transparent,
      body: Container(
        decoration: _pageStyles.dataGridBackground(),
        child: Container(
          margin: EdgeInsets.all(12.0),
          decoration: _pageStyles.dataGridForground(),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(
                child: BasketDataGrid(
                  basketItems: basketData,
                ),
              ),
              // controller.obx(
              //   (data) => Container(
              //     child: BasketDataGrid(
              //       basketItems: controller.basketItem,
              //     ),
              //   ),
              //   onEmpty: Container(
              //     child: BasketDataGrid(
              //       basketItems: controller.basketItem,
              //     ),
              //   ),
              //   // onLoading: Center(
              //   //     child: CircularProgressIndicator(
              //   //   backgroundColor: Get.theme.primaryColorDark,
              //   //   strokeWidth: 9.0,
              //   // )),
              // ),
            ],
          ),
        ),
      ),
    );
  }
}
